<template>
	<view class="coupon-page">
		<!-- 优惠券标签 -->
		<view class="coupon-tabs">
			<view class="tab-item" 
				v-for="tab in couponTabs" 
				:key="tab.type"
				:class="{active: activeTab === tab.type}"
				@click="switchTab(tab.type)">
				<text>{{tab.name}}</text>
			</view>
		</view>

		<!-- 优惠券列表 -->
		<scroll-view class="coupon-list" scroll-y>
			<view class="coupon-item" v-for="coupon in filteredCoupons" :key="coupon.id" @click="viewCouponDetail(coupon.id)">
				<view class="coupon-left">
					<text class="coupon-amount">{{coupon.type === 'discount' ? coupon.discount + '折' : '¥' + coupon.amount}}</text>
					<text class="coupon-condition" v-if="coupon.condition > 0">满{{coupon.condition}}元可用</text>
					<text class="coupon-condition" v-else>无门槛</text>
				</view>
				<view class="coupon-right">
					<text class="coupon-name">{{coupon.name}}</text>
					<text class="coupon-time">{{coupon.startTime}} - {{coupon.endTime}}</text>
					<text class="coupon-status" :class="getStatusClass(coupon.status)">{{getStatusText(coupon.status)}}</text>
				</view>
			</view>

			<!-- 空状态 -->
			<view class="empty-coupon" v-if="filteredCoupons.length === 0">
				<image class="empty-icon" src="/static/my/empty-coupon.png" mode="aspectFit"></image>
				<text class="empty-text">暂无优惠券</text>
				<button class="empty-btn" @click="navigateToGetCoupon">去领取</button>
			</view>
		</scroll-view>

		<!-- 底部领取按钮 -->
		<view class="bottom-bar" v-if="activeTab === 'available'">
			<button class="get-coupon-btn" @click="navigateToGetCoupon">领取优惠券</button>
		</view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

// 优惠券标签
const couponTabs = ref([
	{ type: 'available', name: '可用优惠券' },
	{ type: 'used', name: '已使用' },
	{ type: 'expired', name: '已过期' }
])

const activeTab = ref('available')

// 优惠券数据
const coupons = ref([
	{
		id: 1,
		name: '新用户专享',
		type: 'discount',
		discount: 8.8,
		amount: 0,
		condition: 0,
		startTime: '2023-10-01',
		endTime: '2023-12-31',
		status: 'available'
	},
	{
		id: 2,
		name: '满减优惠券',
		type: 'reduce',
		discount: 0,
		amount: 10,
		condition: 50,
		startTime: '2023-10-01',
		endTime: '2023-10-31',
		status: 'available'
	},
	{
		id: 3,
		name: '折扣券',
		type: 'discount',
		discount: 9.0,
		amount: 0,
		condition: 30,
		startTime: '2023-09-01',
		endTime: '2023-09-30',
		status: 'used'
	},
	{
		id: 4,
		name: '满减券',
		type: 'reduce',
		discount: 0,
		amount: 5,
		condition: 20,
		startTime: '2023-08-01',
		endTime: '2023-08-31',
		status: 'expired'
	}
])

// 过滤后的优惠券
const filteredCoupons = computed(() => {
	return coupons.value.filter(coupon => coupon.status === activeTab.value)
})

// 方法
const switchTab = (tabType) => {
	activeTab.value = tabType
}

const getStatusText = (status) => {
	const statusMap = {
		available: '立即使用',
		used: '已使用',
		expired: '已过期'
	}
	return statusMap[status] || '未知状态'
}

const getStatusClass = (status) => {
	const classMap = {
		available: 'status-available',
		used: 'status-used',
		expired: 'status-expired'
	}
	return classMap[status] || ''
}

const viewCouponDetail = (couponId) => {
	uni.switchTab({
		url: `/pages/order/index`
	})
}

const navigateToGetCoupon = () => {
	uni.showToast({
		title: '跳转到领券中心',
		icon: 'none'
	})
}

onLoad(() => {
	console.log('优惠券页面加载')
})
</script>

<style scoped>
.coupon-page {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding-bottom: 120rpx;
}

/* 优惠券标签 */
.coupon-tabs {
	display: flex;
	background: white;
	border-bottom: 1rpx solid #eee;
}

.tab-item {
	flex: 1;
	text-align: center;
	padding: 30rpx 0;
	font-size: 28rpx;
	color: #666;
	position: relative;
}

.tab-item.active {
	color: #ff6b6b;
	font-weight: bold;
}

.tab-item.active::after {
	content: '';
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 60rpx;
	height: 4rpx;
	background: #ff6b6b;
	border-radius: 2rpx;
}

/* 优惠券列表 */
.coupon-list {
	height: calc(100vh - 120rpx);
	padding: 20rpx;
}

.coupon-item {
	display: flex;
	background: white;
	border-radius: 15rpx;
	margin-bottom: 20rpx;
	overflow: hidden;
	position: relative;
}

.coupon-item::before {
	content: '';
	position: absolute;
	right: 120rpx;
	top: 0;
	bottom: 0;
	width: 20rpx;
	background: 
		radial-gradient(circle at 0 0, transparent 10rpx, #f5f5f5 10rpx) top left,
		radial-gradient(circle at 0 100%, transparent 10rpx, #f5f5f5 10rpx) bottom left;
	background-size: 20rpx 20rpx;
	background-repeat: repeat-y;
}

.coupon-left {
	width: 200rpx;
	background: linear-gradient(135deg, #ff6b6b, #ee5a24);
	padding: 40rpx 20rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	color: white;
}

.coupon-amount {
	font-size: 48rpx;
	font-weight: bold;
	margin-bottom: 15rpx;
}

.coupon-condition {
	font-size: 22rpx;
	opacity: 0.9;
}

.coupon-right {
	flex: 1;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.coupon-name {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 10rpx;
}

.coupon-time {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 15rpx;
}

.coupon-status {
	font-size: 24rpx;
	padding: 8rpx 16rpx;
	border-radius: 20rpx;
	align-self: flex-start;
}

.status-available {
	background: #ff6b6b;
	color: white;
}

.status-used {
	background: #ddd;
	color: #666;
}

.status-expired {
	background: #f5f5f5;
	color: #999;
}

/* 空状态 */
.empty-coupon {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 200rpx 0;
}

.empty-icon {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 40rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
	margin-bottom: 40rpx;
}

.empty-btn {
	background: #ff6b6b;
	color: white;
	border: none;
	border-radius: 50rpx;
	padding: 20rpx 60rpx;
	font-size: 28rpx;
}

/* 底部领取按钮 */
.bottom-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: white;
	border-top: 1rpx solid #eee;
	padding: 20rpx 30rpx;
}

.get-coupon-btn {
	background: #ff6b6b;
	color: white;
	border: none;
	border-radius: 50rpx;
	padding: 25rpx 0;
	font-size: 32rpx;
	font-weight: bold;
}
</style>